﻿using System;
using Caliburn.Micro;
using DomusDotNet.Sbm.Client.SL4.Infrastructure;

namespace DomusDotNet.Sbm.Client.SL4.ViewModels
{
    public class BusyState : PropertyChangedBase
    {
        int busyCount = 0;
        object @lock = new object();
        public bool IsSet { get; private set; }

        public IDisposable Set()
        {
            lock (@lock)
            {
                busyCount++;
            }

            IsSet = true;
            return new DisposableAction(Reset);
        }
        public void Reset()
        {
            bool val;
            lock (@lock)
            {
                busyCount--;
                val = busyCount > 0;
            }

            IsSet = val;
        }
    }

    public interface IHasBusyState
    {
        BusyState BusyState { get; }
    }
}
